from sqlalchemy import Column, String, Integer, ForeignKey, DateTime, literal_column
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, backref

Base = declarative_base()


class User(Base):
    __tablename__ = "user"

    id = Column(Integer, primary_key=True, autoincrement=True)
    username = Column(String(64), unique=True)
    password = Column(String(64))
    email = Column(String(64))
    role = Column(String(64), default="user")

    def __init__(self, username, password, email, role="user"):
        self.username = username
        self.password = password
        self.email = email
        self.role = role


class Book(Base):
    __tablename__ = 'book'

    id = Column(Integer, primary_key=True)
    title = Column(String(100), nullable=False)
    isbn = Column(String(20), unique=True)
    author = Column(String(20))
    count = Column(Integer)

    def __init__(self,title,isbn,author, count):
        self.title=title
        self.isbn=isbn
        self.author=author
        self.count = count

class Borrow(Base):
    __tablename__ = 'borrow'

    id = Column(Integer, primary_key=True)
    user_id = Column(Integer)
    book_id = Column(Integer)
    borrow_time = Column(DateTime)
    return_time = Column(DateTime)

    def __init__(self,user_id,book_id,borrow_time):
        self.user_id = user_id
        self.book_id = book_id
        self.borrow_time = borrow_time
        self.return_time = literal_column('NULL')


class Report(Base):
    __tablename__ = 'report'

    id = Column(Integer, primary_key=True)
    title = Column(String(100), nullable=False)
    content = Column(String(100), nullable=False)


class LMS(Base):
    __tablename__ = 'lms'

    id = Column(Integer, primary_key=True)
    name = Column(String(100), nullable=False)

    def __init__(self,name):
        self.name=name